package client.view;

import java.awt.Color;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import server.model.meeting.User;

public class CreateGroupGui extends JPanel {
	
	private GuiManager manager;
	private JLabel lblInfo;
	private JLabel lblName;
	private JLabel lblListInfo;
	private JLabel lblFeedback;
	
	private JTextField txtName;
	private List lstGroup;
	private List lstUser;
	
	private JButton btnAdd;
	private JButton btnRemove;
	private JButton btnCreateGroup;
	private JButton btnBack;
	
	private Iterator<User> users;


	public CreateGroupGui(GuiManager manager) {
		this.manager = manager;
		setSize(600, 365);
		setLocation(0, 0);
		setVisible(true);
		setLayout(null);

		initGUI();
	}

	private void initGUI() {
		
		lblInfo = new JLabel();
		lblInfo.setText("Create a group");
		lblInfo.setBounds(31, 12, 300, 20);
		lblInfo.setFont(new java.awt.Font("Segoe UI", 1, 14));
		
		lblName = new JLabel("Name :");
		lblName.setBounds(31, 44, 100, 22);
		lblListInfo = new JLabel("Choose a user to add to the group");
		lblListInfo.setBounds(31, 72, 341, 16);
		
		lblFeedback = new JLabel();
		lblFeedback.setBounds(31, 215, 341, 22);
		lblFeedback.setForeground(Color.red);
		
		
		txtName = new JTextField();
		txtName.setBounds(138, 44, 118, 22);
		
		lstUser = new List();
		lstUser.setBounds(31, 100, 130, 102);
		lstUser.setMultipleMode(true);
				
		lstGroup = new List();
		lstGroup.setBounds(234, 94, 130, 102);	
		lstGroup.setMultipleMode(true);
	
		btnAdd = new JButton(">");
		btnAdd.setBounds(173, 119, 50, 22);
		btnAdd.addActionListener(new btnAddParticipantsListener());
		
		btnRemove = new JButton("<");
		btnRemove.setBounds(173, 161, 50, 22);
		btnRemove.addActionListener(new btnRemoveParticipantsListener());
		
		btnCreateGroup = new JButton("Create Group");
		btnCreateGroup.setBounds(244, 249, 120, 22);
		btnCreateGroup.addActionListener(new btnCreateGroupListener());
		
		btnBack = new JButton("<");
		btnBack.setBounds(31, 249, 50, 22);
		btnBack.addActionListener(new btnBackListener());

		add(lblInfo);
		add(lblName);
		add(lblListInfo);
		add(lblFeedback);
		
		add(txtName);

		add(lstGroup);
		add(lstUser);
		
		add(btnAdd);
		add(btnRemove);
		add(btnCreateGroup);	
		add(btnBack);
		
		repaint();
	}
	
	private class btnAddParticipantsListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			
			String [] selectedParticipants = lstUser.getSelectedItems();
			boolean isUsed;
			
			for (int i=0; i<selectedParticipants.length; i++){
				isUsed = false;
				for (int j=0; j<lstGroup.getItems().length; j++){
					if (selectedParticipants[i].matches(lstGroup.getItems()[j])){
						isUsed = true;
					}
				}
				if(!isUsed){
					lstGroup.add(selectedParticipants[i]);
				}
			}			
		}		
	}	
	
	private class btnRemoveParticipantsListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			String [] participants = lstGroup.getSelectedItems();
			for (int i=0; i<participants.length; i++){
				lstGroup.remove(participants[i]);
			}
		}		
	}	
	
	private class btnBackListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			manager.switchScreen("menuGui");
		}		
	}	
	
	private class btnCreateGroupListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			try {
				if (manager.getAdmin().createGroup(txtName.getText(), lstGroup.getItems())){
					manager.switchScreen("menuGui");
				}else{
					lblFeedback.setText("This groupname is already in use");
				}
				
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}		
	}

	public void reset() {
		try {
			lstGroup.removeAll();
			
			int[] selectedIndexes = lstUser.getSelectedIndexes();
			for (int i=0; i<selectedIndexes.length; i++){
				lstUser.deselect(selectedIndexes[i]);
			}
			
			users = manager.getAdmin().getUsers().iterator();
			while (users.hasNext()){
				User currentUser = users.next();
				lstUser.add(currentUser.getName());
			}
		} catch (RemoteException e) {			
			e.printStackTrace();
		}			
	}	
}
